package com.dream.xcarrecorder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.dream.libxrec.player.MP4Player;
import com.dream.libxrec.view.YUVGLSurfaceView;

import java.text.Format;
import java.util.Locale;

public class MP4PlayerActivity extends Activity implements View.OnClickListener{

    private final static String KEY_PATH = "KEY_PATH";

    private MP4Player mPlayer = new MP4Player();
    private SeekBar mSeekBar = null;
    private TextView mDurationTV = null;
    private boolean isTouchSeekBar = false;
    private String mDurationTime = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

        String path = getIntent().getStringExtra(KEY_PATH);

        initView();

        mPlayer.setPath(path);
        mPlayer.setListener(new MP4Player.IPlayerListener() {
            @Override
            public void onReady(long durationUs, boolean isAudio,
                                boolean isVideo) {
                long duration = (int) (durationUs/1000000);//秒
                mSeekBar.setMax((int)duration);
                mDurationTime = String.format(Locale.CANADA,
                        "/%02d" +
                        ":%02d",
                        duration/60,duration%60);
                mDurationTV.setText("00:00"+mDurationTime);
            }

            @Override
            public void onProgressUs(long progressUs) {
                if (isTouchSeekBar){
                    return;
                }

                int p = (int) (progressUs/1000000);
                if (mSeekBar.getProgress() != p){
                    runOnUiThread(() -> {
                        mSeekBar.setProgress(p);
                        String duration = String.format(Locale.CANADA,
                                "%02d:%02d%s"
                                , p/60
                                ,p%60
                                ,mDurationTime);
                        mDurationTV.setText(duration);
                    });
                }
            }

            @Override
            public void onFinish() {

            }
        });

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar,
                                          int progress,
                                          boolean fromUser) {}

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isTouchSeekBar = true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mPlayer.seekTo(seekBar.getProgress()*1000000L);
                isTouchSeekBar = false;
            }
        });
    }

    private void initView() {
        YUVGLSurfaceView mYUVSurface =
                findViewById(R.id.yuv_surface_view);

        findViewById(R.id.btn_player_key).setOnClickListener(this);
        findViewById(R.id.btn_player_pause).setOnClickListener(this);
        findViewById(R.id.btn_player_mute).setOnClickListener(this);

        findViewById(R.id.btn_player_rate2x).setOnClickListener(this);
        findViewById(R.id.btn_player_rate_0_5x).setOnClickListener(this);
        findViewById(R.id.btn_player_rate_1x).setOnClickListener(this);
        mSeekBar = findViewById(R.id.seekBar);
        mDurationTV = findViewById(R.id.duration);

        mYUVSurface.setVisibility(View.VISIBLE);
        mYUVSurface.getHolder().addCallback(new SurfaceHolder.Callback2() {
            @Override
            public void surfaceRedrawNeeded(@NonNull SurfaceHolder holder) {}

            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
                mPlayer.setSurface(mYUVSurface);
                mPlayer.play();
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {}

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {

            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mPlayer != null){
            mPlayer.stop();
            mPlayer = null;
        }
    }

    public static void startActivity(Context ct, String path){
        Intent i = new Intent(ct, MP4PlayerActivity.class);
        i.putExtra(KEY_PATH,path);

        ct.startActivity(i);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btn_player_key){
            mPlayer.setKeyFrameMode(!mPlayer.isKeyFrameMode());
        }else if (id == R.id.btn_player_pause){
            mPlayer.onPause(!mPlayer.isPause());
        }else if (id == R.id.btn_player_rate2x){
            mPlayer.setPlayRate(2);//快进2倍数播放
        }else if (id == R.id.btn_player_rate_0_5x){
            mPlayer.setPlayRate(0.5f);//快进2倍数播放
        }else if (id == R.id.btn_player_rate_1x){
            mPlayer.setPlayRate(1f);//1倍数正常播放
        }else if (id == R.id.btn_player_mute){
            mPlayer.setMute(!mPlayer.isMute());
        }
    }
}
